home *** CD-ROM | disk | FTP | other *** search
- /*
- File: CAS_Main.c
-
- Contains: Main, init, and event loop.
-
- Written by: David H Nelson
-
- Copyright © 1993-1995 ComponentWorks, All rights reserved.
-
- Change History (most recent first):
-
- <12> 05/19/95 SJF Allow null/idle events to be propagated
- ---------------
-
- <7> 05/13/95 RB As Per the CALib pre-alpha code review, changed
- main to call CAUtil_IsCALibPresent()
- to determine CALib availablity.
- ---------------
- <6> 04/22/95 RB Changed main event loop to contain only one
- call to CADispatchEvent()
- ---------------
- <5> 02/25/95 RB Hacked up main event loop to pass events
- to the OpenDoc dispatcher
- ---------------
- <4> 01/23/95 SJF Add CALib support
- ---------------
- <3> 01/20/95 RB Removed call to appAdjustCursor from the
- main event loop. It was being called everytime
- through, now it's only called when a mouseMovedMessage
- occurs.
- ---------------
- <2> 01/17/95 DAS changed all FrontWindow() calls to
- App_GetFrontDocWindow() to account for
- floating windows.
- 01/15/95 DAS hacked in ToolWindow event handling in one location
- 12/22/94 DHN Created from existing Light software
- application.
- ---------------
- <1> 11/20/94 DHN Created.
- */
-
- #define _MAIN_
-
- #ifdef USE_CALIB
- #include "CALib.h"
- #include "CAS_CAUtil.h"
- #endif
-
- #include "CAS_Globals.h"
- #include "CAS_Misc.h"
- #include "CAS_Main.h"
- #include "CAS_App.h"
- #include "CAS_Event.h"
- #include "CAS_ToolPalette.h"
- #include "CAS_Menu.h"
-
- #include "TraceLog.h"
-
- //----------------------------------------------------------------------
- // local prototypes
-
- #if defined(__cplusplus)
- extern "C"
- {
- #endif
-
- static void doEventLoop( void );
- static void InitMacintosh( void );
- static void InitGlobals( void );
- static void DisposeGlobals( void );
- static Boolean checkCALibGestalt(void);
-
- #if defined(__cplusplus)
- }
- #endif
-
- //---------------------------------------------------------------------------
- // Main event loop. Keeps looping until the users decides to quit. Dispatches each
- // event type to the appropriate handler after setting gOptionPressed, gShiftPressed
- // gCmdPressed, and gControlPressed. Calls the doIdle routine, appAdjustCursor,
- // and adjustMenus on each loop.
-
- static void doEventLoop( void )
- {
- EventRecord theEvent;
- WindowPtr theWindow;
- Boolean eventHandled;
- CAEventInfo eventInfo;
- Boolean result;
-
- /* was FrontWindow() */
- theWindow = App_GetFrontDocWindow();
-
- // use theEvent.where (the last mouse position in global coords).
- App_AdjustCursor( theWindow, theEvent.where, gMouseRgn );
-
- while (!gExitFlag)
- {
- Event_DoIdle();
-
- /* was FrontWindow() */
- theWindow = App_GetFrontDocWindow();
-
- // use theEvent.where (the last mouse position in global coords).
- //appAdjustCursor(theWindow, theEvent.where, gMouseRgn);
- //Menu_AdjustMenus( theWindow );
-
-
- result = WaitNextEvent( everyEvent, &theEvent, gSleepTime, gMouseRgn );
-
- {
-
- if (ToolPalette_IsEvent( nil, &theEvent ))
- if (ToolPalette_DoEvent( nil, &theEvent ))
- continue;
-
- gOptionPressed = ((theEvent.modifiers & optionKey) != 0);
- gShiftPressed = ((theEvent.modifiers & shiftKey) != 0);
- gCmdPressed = ((theEvent.modifiers & cmdKey) != 0);
- gControlPressed = ((theEvent.modifiers & controlKey) != 0);
- gSleepTime = CAGetSleepTime();
-
-
- #ifdef USE_CALIB
- if (gCALibExists)
- {
- eventHandled = CADispatchEvent( &theEvent, &eventInfo );
-
- if (eventHandled)
- continue;
-
- }
- #endif
-
- switch (theEvent.what)
- {
-
- #ifdef USE_CALIB
-
- case kCAEvtMouseDownEmbedded:
- eventHandled = CAUtil_HandleMouseDownEmbedded (&theEvent, &eventInfo);
- break;
-
- case kCAEvtMouseUpEmbedded:
- eventHandled = CAUtil_HandleMouseUpEmbedded (&theEvent, &eventInfo);
- break;
-
- case kCAEvtMouseUpBorder:
- break;
-
- case kCAEvtMouseDownBorder:
- eventHandled = CAUtil_HandleMouseDownBorder (&theEvent, &eventInfo);
- break;
-
- case kCAEvtBGMouseDownEmbedded:
- eventHandled = CAUtil_HandleMouseDownEmbedded (&theEvent, &eventInfo);
- break;
-
- #endif
-
- case nullEvent:
- Event_HandleNullEvent( &theEvent );
- break;
-
- case mouseDown:
- Event_HandleMouseDownEvent( &theEvent );
- break;
-
- case mouseUp:
- Event_HandleMouseUpEvent( &theEvent );
- // Rick - Added this to force a mouseMovedMessage
- // So the cursor get updated
- SetRectRgn( gMouseRgn, 0, 0, 1, 1 );
- break;
-
- case keyDown:
- Event_HandleKeyDownEvent( &theEvent );
- break;
-
- case autoKey:
- Event_HandleAutoKeyEvent( &theEvent );
- break;
-
- case keyUp:
- Event_HandleKeyUpEvent( &theEvent );
- break;
-
- case activateEvt:
- Event_HandleActivateEvent( &theEvent );
- break;
-
- case updateEvt:
-
- // After CADispatchEvent the update region is
- // the original minus any area used by embedded parts
-
- Event_HandleUpdateEvent( &theEvent );
-
-
- break;
-
- case kHighLevelEvent:
-
- Event_HandleHighLevelEvent( &theEvent );
- break;
-
- case osEvt:
-
- Event_HandleOSEvent( &theEvent );
- break;
-
- case diskEvt:
- Event_HandleDiskEvent( &theEvent );
- break;
-
- default:
- break;
- }
-
- }
-
-
- //SectRgn(gMouseRgn, CAGetMouseRegion(), gMouseRgn);
-
- }
-
-
- }
-
-
- //---------------------------------------------------------------------------
- // Initialize all of the managers and set the gestalt theDoc.
-
- static void InitMacintosh( void )
- {
- EventRecord anyEvent;
- short i;
-
- MaxApplZone();
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
-
- // we must call EventAvail 3 times so the process mgr knows that
- // we're alive and will make the Scrap purgable.
- for (i=0; i<3; i++)
- EventAvail(everyEvent, &anyEvent);
-
- // flush up to the next null event.
- FlushEvents( everyEvent, nullEvent );
- InitCursor();
- }
-
- //---------------------------------------------------------------------------
- // Initialize all theDoc.
-
- static void InitGlobals( void )
- {
- gExitFlag = false;
- gInBackground = false;
-
- gOptionPressed = false;
- gShiftPressed = false;
- gCmdPressed = false;
- gControlPressed = false;
- gMouseUpTickCount = 0;
- gMouseUpPoint.h = gMouseUpPoint.v = 0;
-
- gSleepTime = GetCaretTime();
- gMouseRgn = NewRgn();
-
- gPrefRefNum = 0;
- gPrefChanged = false;
-
- gWindowCount = 0;
- }
-
- //---------------------------------------------------------------------------
- // dispose all theDoc allocated in initGlobals.
-
- static void DisposeGlobals( void )
- {
- if (gMouseRgn != nil)
- {
- DisposeRgn( gMouseRgn );
- gMouseRgn = nil;
- }
- }
-
- //---------------------------------------------------------------------------
- // This routine will check the CALib gestalt version
-
- static Boolean checkCALibGestalt(void)
- {
- #if 0
- OSErr errStatus;
- long result;
-
- errStatus = Gestalt( gestaltCALibVersion, &result );
-
- return( (errStatus == noErr) && (result >= CAUtil_VERSION_1) );
- #else
- return(true);
- #endif
- }
-
- //---------------------------------------------------------------------------
- // Main entry point for application. Clears the theDoc. Calls InitMacintosh,
- // setUpMenus, and if App_Init returns noErr calls doEventLoop. Calls appQuit
- // when ready to quit.
-
- void main( void )
- {
- InitMacintosh(); // Initialize all MacOS Managers
-
- InitGlobals(); // Initialize app’s global variable
-
- InitTools(); // Initialize our utility routines
-
- #if TRACING
- Trace_StartLog(NULL, true);
- #endif
-
- #ifdef USE_CALIB
- {
- // Check to see if CALib exists.
- gCALibExists = CAUtil_IsCALibPresent();
-
- if (gCALibExists)
- {
-
- OSErr theErr;
-
- theErr = CAUtil_InitCALib();
-
- if (theErr)
- ;
-
- }
- }
- #endif
-
- Menu_SetUpMenus(); // Set up the app’s menu's
-
- if (App_Init() == noErr) // Let the app initialize anything it needs
- {
- doEventLoop(); // Here we go
-
- #ifdef USE_CALIB
- if (gCALibExists)
- {
- OSErr theErr;
- theErr = CAUtil_ShutdownCALib();
-
- }
- #endif
-
- #if TRACING
- Trace_StopLog();
- #endif
-
- App_Quit(); // Let the app clean up before quit
- DisposeGlobals();
- }
-
- }
-
-